package com.haha.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haha.community.entity.DiscussPost;
import com.haha.community.dao.DiscussPostMapper;
import com.haha.community.service.DiscussPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.community.util.SensitiveFilterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yang
 * @since 2020-10-30
 */
@Service
public class DiscussPostServiceImpl extends ServiceImpl<DiscussPostMapper, DiscussPost> implements DiscussPostService {
    @Autowired
    private SensitiveFilterUtil sensitiveFilterUtil;

    @Override
    public List<DiscussPost> findDiscussPosts(int userId, int offset, int limit) {
        QueryWrapper<DiscussPost> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("status",2);
        if (userId!=0){
            queryWrapper.eq("user_id",userId);
        }
        queryWrapper.orderByDesc("type","create_time");
        return baseMapper.selectByPage(queryWrapper, offset, limit);
    }

    @Override
    public int findDiscussPostRow(int userId) {
        QueryWrapper<DiscussPost> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("status",2);
        if (userId!=0){
            queryWrapper.eq("user_id",userId);
        }
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public void addDiscussPost(DiscussPost post) {
        //html先转义，然后过滤
        String content = HtmlUtils.htmlEscape(post.getContent());
        String title = HtmlUtils.htmlEscape(post.getTitle());
        post.setContent(sensitiveFilterUtil.filter(content));
        post.setTitle(sensitiveFilterUtil.filter(title));
        post.setCreateTime(new Date());
        baseMapper.insert(post);
    }
}
